<?php
/**
 * Created by PhpStorm. User: ADun, Date: 14/12/7 下午12:12
 * Description:
 */
require_once __DIR__.DIRECTORY_SEPARATOR.'linhePDO.php';

class Hermes {
    public static function 打印测试代码($file_path,$class_name,$strap=null,$autho='yang'){

        /*
        if ($argc < 3) {
            die("Usage: php $argv[0] <file_path> <class_name> [bootstrap] [author = dogstar]\n");
        }
        */
        $filePath = $file_path;
        $className =$class_name;
        $bootstrap =$strap;
        $author = $autho;

        if (!empty($bootstrap)) {
            require $bootstrap;
        }

        require $filePath;

        if (!class_exists($className)) {
            die("Error: cannot find class($className). \n");
        }

        $reflector = new ReflectionClass($className);

        $methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);

        date_default_timezone_set('Asia/Shanghai');
        $objName = lcfirst(str_replace('_', '', $className));

        $code = "<?php
/**
 * PhpUnderControl_AppAds_Test
 *
 * 针对 $filePath $className 类的PHPUnit单元测试
 *
 * @author: $author " . date('Ymd') . "
 */

";

        if (file_exists(dirname(__FILE__) . '/test_env.php')) {
            $code .= "require_once dirname(__FILE__) . '/test_env.php';
";
        }

        $code .= "
if (!class_exists('$className')) {
    require dirname(__FILE__) . '/' . '$filePath';
}

class PhpUnderControl_" . str_replace('_', '', $className) . "_Test extends PHPUnit_Framework_TestCase
{
    public \$$objName;

    protected function setUp()
    {
        parent::setUp();

        \$this->$objName = new $className();
    }

    protected function tearDown()
    {
    }

";

        foreach ($methods as $method) {
            if($method->class != $className) continue;

            $fun = $method->name;
            $Fun = ucfirst($fun);

            if (strlen($Fun) > 2 && substr($Fun, 0, 2) == '__') continue;

            $rMethod = new ReflectionMethod($className, $method->name);
            $params = $rMethod->getParameters();
            $isStatic = $rMethod->isStatic();
            $isConstructor = $rMethod->isConstructor();
            $isPrivate=$rMethod->isPrivate();

            if($isConstructor OR $isPrivate) continue;

            $initParamStr = '';
            $callParamStr = '';
            foreach ($params as $param) {
                $initParamStr .= "
        \$" . $param->name . " = '';";
                $callParamStr .= '$' . $param->name . ', ';
            }
            $callParamStr = empty($callParamStr) ? $callParamStr : substr($callParamStr, 0, -2);

            $code .= "
    /**
     * @group returnFormat
     */
    public function test$Fun" . "ReturnFormat()
    {" . (empty($initParamStr) ? '' : "$initParamStr\n") . '
        ' . ($isStatic ? "\$rs = $className::$fun($callParamStr);": "\$rs = \$this->$objName->$fun($callParamStr);") . "
    }
";
            $code .= "
    /**
     * @depends test$Fun" . "ReturnFormat
     * @group businessData
     */
    public function test$Fun" . "BusinessData()
    {" . (empty($initParamStr) ? '' : "$initParamStr\n") . '
        ' . ($isStatic ? "\$rs = $className::$fun($callParamStr);": "\$rs = \$this->$objName->$fun($callParamStr);") . "
        assert(\$rs,'');
    }
";
        }

        $code .= "
}";

        echo $code;
        echo "\n";
    }

    /**
     * 根据数据库表打印对应的BO
     * @param null|string $tblName
     * @param string $schema
     */
    public static function 数据库表生成DO($tblName=null,$schema=G_DB_NAME)
    {

        $single = false;
        if (is_null($tblName)) {
            $sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '".$schema."'";
            echo $sql;
            $tbs = pdo()->selectAll($sql);
        } else {
            $tbs = array('TABLE_NAME' => $tblName);
            $single = true;
        }
        foreach ($tbs as $t) {
            if ($single)
                $tname = $t;
            else
                $tname = $t['TABLE_NAME'];

            $className = ucfirst($tname);

            echo "class Do{$className} extends DataObject {\n";

            $sql = "select column_name  from information_schema.columns where table_schema='{$schema}' and table_name='{$tname}' ORDER BY ordinal_position";
            $clm = pdo()->selectAll($sql);

            $constructF = " public function __construct(";
            //$constC=" parent:: __construct(); foreach(func_get_args() as \$k=>\$v){if(!is_null(\$v)) \$this->data[\$k]=\$v;}";
            $constC = " ";
            $functions = " public function tableName(){ return '{$tname}';}\n";
            $flag = false;//首字段前边不加逗号

            foreach ($clm as $c) {
                $cname = $c['column_name'];
                if ($flag)
                    $constructF .= ",";
                $flag = true;
                $constructF .= "\$" . $cname . "=null";
                $constC .= "\t if(!is_null(\${$cname})) \$this->set_{$cname}(\${$cname});";
                $functions .= " public function set_{$cname}(\$value){ \$this->data['{$cname}'] = \$value; }\n";
                $functions .= " public function get_{$cname}(\$isValue=true){ if(\$isValue) return \$this->data['{$cname}'];  else return'{$cname}'; }\n";
            }

            $constructF .= "){\n";
            echo $constructF;
            echo $constC;
            echo "}\n";
            echo $functions;
            echo "}\n";
        }
    }
}